﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace BanHangCshap.Class
{
    class DotKhuyenMai:Database
    {
        private string _madot;
        public string madot
        {
            get
            {
                return _madot;
            }
            set
            {
                _madot = value;
            }
        }
        private string _tendot;
        public string tendot
        {
            get
            {
                return _tendot;
            }
            set
            {
                _tendot = value;
            }
        }
        public DateTime _ngaybd;
        public DateTime ngaybd
        {
            get
            {
                return _ngaybd;
            }
            set
            {
                _ngaybd = value;
            }
        }
        public DateTime _ngaykt;
        public DateTime ngaykt
        {
            get
            {
                return _ngaykt;
            }
            set
            {
                _ngaykt = value;
            }
        }
        private string _tendn;
        public string tendn
        {
            get
            {
                return _tendn;
            }
            set
            {
                _tendn = value;
            }
        }
        private Boolean _kmtheodoanhso;
        public Boolean kmtheodoanhso
        {
            get
            {
                return _kmtheodoanhso;
            }
            set
            {
                _kmtheodoanhso = value;
            }
        }
        private double _doanhso;
        public double doanhso
        {
            get
            {
                return _doanhso;
            }
            set
            {
                _doanhso = value;
            }
        }
        private string _ghichu;
        public string ghichu
        {
            get
            {
                return _ghichu;
            }
            set
            {
                _ghichu = value;
            }
        }
        public DotKhuyenMai()
        {
        }
        public DotKhuyenMai(string madot)
        {
            string sql = "select * from DotKhuyenMai where madot = '" + madot + "'";
            foreach (DataRow dr in getDataTable(sql).Rows)
            {
                this.madot = madot;
                this.tendot = dr["tendot"].ToString().Trim();
                this.ngaybd = (DateTime)dr["ngaybd"];
                this.ngaykt = (DateTime)dr["ngaykt"];
                //this.kmtheodoanhso = (Boolean)dr["kmtheodoanhso"];
                this.doanhso = double.Parse(dr["doanhso"].ToString().Trim());
                this.ghichu = dr["ghichu"].ToString().Trim();
                this.tendn = dr["tendn"].ToString().Trim();
            }

        }
        public DataTable layDSDotKhuyenMai()
        {
            string sql = "select * from DotKhuyenMai order by madot desc";
            return getDataTable(sql);
        }
        public DataTable layDSKhachHangDuCT()
        {
            string sql = "select distinct a.madot, b.makhncc, tendot, tenkhncc, ngaydangky from DotKhuyenMai a, DangKyDotKhuyenMai b, KhachHangNCC c where a.madot = b.madot and b.makhncc = c.makhncc order by a.madot desc";
            return getDataTable(sql);
        }
        public DataTable layDSDotKhuyenMaiConHan()
        {
            string sql = "select * from DotKhuyenMai where convert(DateTime,'" + DateTime.Now.ToString("dd/MM/yyyy") + "',103 ) >= ngaybd and convert(DateTime,'" + DateTime.Now.ToString("dd/MM/yyyy") + "',103 ) <= ngaykt  order by madot desc";
            return getDataTable(sql);
        }
        public DataTable layDSDotKMChiTiet(string madot)
        {
            string sql = "select a.mahang,a.dvt,soluong, dongia,thanhtien from DotKhuyenMaiCT a left join HangHoa b on a.mahang = b.mahang where madot = '" + madot + "' order by id";
            return getDataTable(sql);
        }
        public Boolean themDotKhuyenMai()
        {
            string sql = "insert into DotKhuyenMai(madot,tendot,ngaybd, ngaykt, doanhso,ghichu,tendn) values ('" + madot + "',N'" + tendot + "','" + ngaybd.ToString("yyyy/MM/dd") + "','" + ngaykt.ToString("yyyy/MM/dd") + "', " + doanhso + ",N'" + ghichu + "','" + tendn + "')";
            return executeQueryTrans(sql);
            
        }
        public Boolean themDotKhuyenMaiCT(string madot, string mahang,string dvt, double soluong, double dongia, double thanhtien)
        {
            string sql = "insert into DotKhuyenMaiCT(madot, mahang,dvt, soluong, dongia, thanhtien) values ('" + madot + "', '" + mahang + "',N'" + dvt + "'," + soluong + ", " + dongia + ", " + thanhtien + ")";
            return executeQueryTrans(sql);
        }
        public Boolean xoaDotKhuyenMai(string madot)
        {
            string sql = "delete DotKhuyenMai where madot = '" + madot + "'";
            return executeQueryTrans(sql);
        }
        public Boolean dangKyDuDotKhuyenMai(string madot, string makhncc, string ngaydangky)
        {
            string sql = "insert into DangKyDotKhuyenMai(madot, makhncc, ngaydangky) values ('" + madot + "', '" + makhncc + "','" + ngaydangky + "')";
            return executeQuery(sql);
        }
        public Boolean xoaDangKyDotKhuyenMai(string madot, string makhncc)
        {
            string sql = "delete DangKyDotKhuyenMai where madot = '" + madot + "' and makhncc = '" + makhncc + "'";
            return executeQueryTrans(sql);
        }
    }
}
